package tuling.dubbo.client;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import tuling.dubbo.server.UserService;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class YoungClient {
    //基于URL构建远程服务
    public UserService buildRemoteServices(String remoteUrl) {
        ReferenceConfig<UserService> referenceConfig = new ReferenceConfig();
        referenceConfig.setInterface(UserService.class);
        referenceConfig.setRegistry(new RegistryConfig("multicast://224.1.2.3:11111"));
        referenceConfig.setUrl(remoteUrl);

        referenceConfig.setApplication(getApplicationConfig());

        referenceConfig.setLoadbalance("roundrobin");

        return referenceConfig.get();

    }

   private ApplicationConfig getApplicationConfig(){
       ApplicationConfig applicationConfig = new ApplicationConfig("young-app");
       applicationConfig.setQosEnable(false);

       return applicationConfig;
   }

    public static void main(String[] args) throws IOException {
        YoungClient youngClient = new YoungClient();
        String cmd;
        UserService userService = youngClient.buildRemoteServices(null);
        int count = 0;
        while(!"exit".equals(cmd=read())){
            System.out.println(userService.getUser(980));
        }




    }

    private static String read() throws IOException {
        byte[] b = new byte[1024];
        LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(System.in));
        return lineNumberReader.readLine();
    }
}
